﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Validators
/// </summary>
public static class validators
{
    /// <summary>
    /// Validate a string if it is valid or not
    /// Used to validate names and addresses
    /// </summary>
    /// <param name="Str"></param>
    /// <returns></returns>
    public static bool IsValidString(string Str)
    {
        if (string.IsNullOrEmpty(Str)) return false;
        //Valid character set, actually its a process of white list validation
        string ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._- ,1234567890?![]";
        //check if there is any invalid character in the string
        foreach (char c in Str)
        {
            if (ValidChars.IndexOf(c) < 0) return false;
        }
        return true;
    }

    public static bool IsValidPhoneNumber(string PhoneNumber)
    {
        if (string.IsNullOrEmpty(PhoneNumber)) return false;
        if (PhoneNumber[0] != '+') return false;
        if (PhoneNumber.Length < 6) return false;
        //Valid character set, actually its a process of white list validation
        string ValidChars = "1234567890";
        PhoneNumber = PhoneNumber.Substring(1);
        //check if there is any invalid character in the string
        foreach (char c in PhoneNumber)
        {
            if (ValidChars.IndexOf(c) < 0) return false;
        }
        return true;
    }

    /// <summary>
    /// Checkes if a string is valid or not
    /// </summary>
    /// <param name="email"></param>
    /// <returns></returns>
    public static bool IsValidEmail(string email)
    {
        try
        {
            //Check if System.Net.Mail accepts it as a valid email address
            System.Net.Mail.MailAddress mail = new System.Net.Mail.MailAddress(email);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public static bool IsValidURL(string url)
    {
        try
        {
            Uri uri = new Uri(url);
            string host = uri.Host;
            return true;
        }
        catch
        {
            return false;
        }
    }

    public static bool IsValidIP(string ip)
    {
        try
        {
            System.Net.IPAddress Ip = System.Net.IPAddress.Parse(ip);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public static bool IsValidInt(string Int)
    {
        try
        {
            int a = int.Parse(Int);
            return true;
        }
        catch
        {
            return false;
        }
    }

}